package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* LocationView2
* */
class LocationView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_E80958)
        }
    }

    private val mPaint4 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_46D693)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }

    private val mPath3 by lazy {
        Path()
    }

    private val mPath4 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            save()
            mPath0.apply {
                addRoundRect(
                    -cx,
                    -cy,
                    cx ,
                    cx,
                    cx / 5,
                    cy / 5,
                    Path.Direction.CW
                )
            }

            mPath1.apply {
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )
                clipPath(mPath0)
            }
            drawPath(mPath1, mPaint0)
            restore()

            mPath2.apply {
                addCircle(
                    0F,
                    -cy * 2 / 7,
                    cx / 5,
                    Path.Direction.CW
                )
            }
            mPath3.apply {
                arcTo(
                    -cx * 4 / 7,
                    -cy * 6 / 7,
                    cx * 4 / 7,
                    cy * 2 / 7,
                    160F,
                    220F,
                    true
                )
                //lineTo(0F, cy * 6 / 7)
                quadTo(
                    cx * 3 / 7,
                    cy * 2 / 7,
                    0F,
                    cy * 6 / 7
                )

                quadTo(
                    -cx * 3 / 7,
                    cy * 2 / 7,
                    cx * 4 / 7 * Math.cos(Math.toRadians(160.0)).toFloat(),
                    cy * 4 / 7 * Math.sin(Math.toRadians(160.0)).toFloat() - cy * 2 / 7
                )
                //close()
            }

            save()
            mPath4.apply {
                fillType = Path.FillType.WINDING
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CCW
                )
                addPath(mPath2)
                clipPath(mPath3)
            }
            drawPath(mPath4, mPaint4)
            restore()

            restore()
        }
    }
}